package top.malegod.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.malegod.mapper.OrderMapper;
import top.malegod.model.Comments;
import top.malegod.model.Order;
import top.malegod.service.OrderService;
import top.malegod.utils.ServerResponse;
import top.malegod.utils.Utils;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/5/23 13:46
 * @description: 订单实现service接口
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 获取用户未付款订单service层操作
     */
    @Override
    public ServerResponse<Order> getUnpaidOrder(String userId) {
        Order order = orderMapper.findOrderByUserId(userId);
        if (order == null) {
            return ServerResponse.createError("error");
        }
        return ServerResponse.createSuccess(order);
    }

    /**
     * 获取订单详细信息
     */
    @Override
    public ServerResponse<Order> findOrder(String userId, String id) {
        Order order = orderMapper.findOrder(userId, id);
        return ServerResponse.createSuccess(order);
    }

    /**
     * 获取所有订单
     */
    @Override
    public ServerResponse<Order> findAllOrder(String userId) {
        List<Order> orders = orderMapper.findAllOrderByUserId(userId);
        return ServerResponse.createSuccess(orders);
    }

    /**
     * 创建订单service层操作
     */
    @Override
    public ServerResponse createOrder(Order order) {
        order.setId(Utils.createUUID());
        int success = orderMapper.addOrder(order);
        if (success == 0) {
            return ServerResponse.createError("error");
        }
        return ServerResponse.createSuccess("success");
    }

    /**
     * 取消订单
     */
    @Override
    public ServerResponse cancelOrder(String id, String userId) {
        int success = orderMapper.update(id, userId);
        if (success == 0) {
            return ServerResponse.createError("error");
        }
        return ServerResponse.createSuccess("success");
    }

    /**
     * 删除订单
     */
    @Override
    public ServerResponse deleteOrder(String id) {
        int success = orderMapper.deleteOrder(id);
        if (success == 0) {
            return ServerResponse.createError("error");
        }
        return ServerResponse.createSuccess("success");
    }

    /**
     * 评论订单
     */
    @Override
    public ServerResponse commentsOrder(Comments comments) {
        int success = orderMapper.addCommentsOrder(comments);
        if (success == 0) {
            return ServerResponse.createError("error");
        }
        int count = orderMapper.commentsOrderSuccess(comments.getOrderId());
        return ServerResponse.createSuccess("success");
    }

    @Override
    public ServerResponse<Order> getOngoingOrder(String userId) {
        Order order = orderMapper.findOngoingOrderByUserId(userId);
        if (order == null) {
            return ServerResponse.createError("error");
        }
        return ServerResponse.createSuccess(order);
    }

    @Override
    public ServerResponse<Order> getOpenOrder(String userId) {
        Order order = orderMapper.findOpenOrderByUserId(userId);
        if (order == null) {
            return ServerResponse.createError("error");
        }
        return ServerResponse.createSuccess(order);
    }


}
